package com.yitao.cms.service.product;

import com.skipper.base.json.JSON;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.constant.product.ProductRelevanceInfoEnums;
import com.yitao.client.dto.product.ProductRelevanceInfoDto;
import com.yitao.client.model.product.ProductRelevanceInfoModel;
import com.yitao.client.model.product.QProductRelevanceInfoModel;
import com.yitao.cms.utils.BaseUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.persistence.EntityManager;
import java.util.List;

/**
 * @Author: wwl
 * @Date: 2020/4/10 18:23
 * @Description:商品关联信息
 */
@Service
public class ProductRelevanceInfoService extends GenericEntityService<ProductRelevanceInfoModel, ProductRelevanceInfoDto, QProductRelevanceInfoModel> {
    public ProductRelevanceInfoService(EntityManager entityManager) {
        super(entityManager);
    }
    private BaseUtils baseUtils = BaseUtils.getInstance();

    /**
     * 更新产品关联URL
     * @param productId
     * @param type
     * @param relevanceInfo
     * @param userId
     * @return
     */
    @Transactional
    public ProductRelevanceInfoModel saveProductRelevanceInfo(Long productId, ProductRelevanceInfoEnums.TYPE type, String relevanceInfo, Long userId){
        List<ProductRelevanceInfoModel> list = repository.findAll(Q.productId.eq(productId).and(Q.type.eq(type.getCode())).and(Q.deleteStatus.eq(false)));
        ProductRelevanceInfoModel productRelevanceInfoModel = new ProductRelevanceInfoModel();
        if (!CollectionUtils.isEmpty(list) && list.size()==1){//只有一条时更新
            BeanUtils.copyProperties(list.get(0),productRelevanceInfoModel);
            productRelevanceInfoModel.setRelevantInfo(relevanceInfo);
            productRelevanceInfoModel.setLastUpdateBy(userId);
            productRelevanceInfoModel.setLastUpdateTime(baseUtils.getNow());
        }else{
            if (list.size()>1){//多条时需删除之前的数据
                repository.updateByWhere(Q.productId.eq(productId).and(Q.type.eq(type.getCode()).and(Q.deleteStatus.eq(false)))
                        ,it-> it.set(Q.deleteStatus, true));
            }
            productRelevanceInfoModel.setProductId(productId);
            productRelevanceInfoModel.setType(type.getCode());
            productRelevanceInfoModel.setRelevantInfo(relevanceInfo);
            productRelevanceInfoModel.setDeleteStatus(false);
            productRelevanceInfoModel.setCreateBy(userId);
            productRelevanceInfoModel.setCreateTime(baseUtils.getNow());
            productRelevanceInfoModel.setLastUpdateBy(userId);
            productRelevanceInfoModel.setLastUpdateTime(baseUtils.getNow());
        }
        logger.info(JSON.toJSONString(productRelevanceInfoModel));
        ProductRelevanceInfoModel productRelevanceInfoModel1 = repository.save(productRelevanceInfoModel);
        return productRelevanceInfoModel1;
    }

    /**
     * 查询关联信息
     * @param productId
     * @param type
     * @return
     */
    public String findRelevanceInfoByProductId(Long productId,ProductRelevanceInfoEnums.TYPE type){
        String relevanceInfo = null;
        if (productId!=null && type.getCode()!=null){
            relevanceInfo = queryFactory.select(Q.relevantInfo).from(Q).where(Q.productId.eq(productId)
                    .and(Q.type.eq(type.getCode())).and(Q.deleteStatus.eq(false))).fetchFirst();
        }
        return relevanceInfo;
    }
}
